
import ctypes
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
import ttkbootstrap as ttk
from ttkbootstrap.constants import *


class WinGUI(ttk.Window):
    def __init__(self):
        super().__init__()
        self.__win()
        self.canvas = self.__tk_canvas_image(self)
        self.file_path = self.__tk_label_file_path(self)

    def __win(self):
        hwnd = ctypes.windll.user32.GetParent(self.winfo_id())
        ctypes.windll.user32.SetWindowLongPtrW(hwnd, -20, ctypes.c_long(0x00080000))  # WS_CAPTION
        self.title("ISP")
        # 设置窗口大小、居中
        self.themename = "litera"
        style = ttk.Style()
        theme_names = style.theme_names()  # 以列表的形式返回多个主题名
        style.theme_use("superhero")
        print(theme_names)
        width = 778
        height = 744
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.geometry(geometry)

        self.minsize(width=width, height=height)

    def scrollbar_autohide(self, vbar, hbar, widget):
        """自动隐藏滚动条"""

        def show():
            if vbar: vbar.lift(widget)
            if hbar: hbar.lift(widget)

        def hide():
            if vbar: vbar.lower(widget)
            if hbar: hbar.lower(widget)

        hide()
        widget.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Leave>", lambda e: hide())
        if hbar: hbar.bind("<Enter>", lambda e: show())
        if hbar: hbar.bind("<Leave>", lambda e: hide())
        widget.bind("<Leave>", lambda e: hide())

    def v_scrollbar(self, vbar, widget, x, y, w, h, pw, ph):
        widget.configure(yscrollcommand=vbar.set)
        vbar.config(command=widget.yview)
        vbar.place(relx=(w + x) / pw, rely=y / ph, relheight=h / ph, anchor='ne')

    def h_scrollbar(self, hbar, widget, x, y, w, h, pw, ph):
        widget.configure(xscrollcommand=hbar.set)
        hbar.config(command=widget.xview)
        hbar.place(relx=x / pw, rely=(y + h) / ph, relwidth=w / pw, anchor='sw')

    def create_bar(self, master, widget, is_vbar, is_hbar, x, y, w, h, pw, ph):
        vbar, hbar = None, None
        if is_vbar:
            vbar = Scrollbar(master)
            self.v_scrollbar(vbar, widget, x, y, w, h, pw, ph)
        if is_hbar:
            hbar = Scrollbar(master, orient="horizontal")
            self.h_scrollbar(hbar, widget, x, y, w, h, pw, ph)
        self.scrollbar_autohide(vbar, hbar, widget)

    def __tk_canvas_image(self, parent):
        canvas = Canvas(parent, width=800, height=600)
        canvas.place(relx=0.5, rely=0.5, relwidth=0.8, relheight=0.8, anchor='center')
        return canvas
    def __tk_label_file_path(self, parent):
        label = Label(parent, text="当前图片路径：", anchor="center")
        label.pack(fill="x")
        return label


class Win(WinGUI):
    def __init__(self, controller):
        self.ctl = controller
        super().__init__()
        self.__event_bind()
        self.config(menu=self.create_menu())
        self.ctl.init(self)

    def create_menu(self):
        menu = Menu(self, tearoff=False)
        menu.add_cascade(label=" 文件", menu=self.menu_lqkpe2hn(menu))
        menu.add_command(label=" 撤回", command=self.ctl.undo)
        menu.add_command(label=" 重做", command=self.ctl.cancel)
        menu.add_cascade(label=" 图像", menu=self.menu_lqkphkh7(menu))
        menu.add_cascade(label=" 颜色", menu=self.menu_lqkpkgbv(menu))
        menu.add_cascade(label=" 工具", menu=self.menu_tools(menu))
        # menu.add_cascade(label=" 对比", command=self.ctl.difference)
        # 创建菜单项并绑定事件
        menu.add_command(label=" 对比", command=self.ctl.difference)

        # 绑定悬停事件
        menu_item = menu.entryconfig(" 对比", underline=0)
        menu.bind("<Enter>", self.ctl.difference)
        menu.bind("<Button-1>", self.ctl.difference)  # 当菜单项被选中时触发对比函数
        menu.bind("<Enter>", self.ctl.difference)  # 当鼠标悬停在菜单项上时触发对比函数
        menu.bind("<ButtonRelease-1>", self.ctl.cancel_difference)  # 当松开菜单项时取消对比函数的触发
        menu.bind("<Leave>", self.ctl.cancel_difference)  # 当离开菜单项时取消对比函数的触发
        menu.add_command(label=" 关于", command=self.ctl.about)
        return menu

    def menu_lqkpe2hn(self, parent):
        menu = Menu(parent, tearoff=False)
        menu.add_command(label="打开", command=self.ctl.open_file)
        menu.add_command(label="保存", command=self.ctl.save)
        return menu

    def menu_lqkphkh7(self, parent):
        menu = Menu(parent, tearoff=False)
        menu.add_command(label=" 裁剪", command=self.ctl.crop_image)
        menu.add_command(label=" 旋转", command=self.ctl.rotate_image)
        menu.add_command(label=" 镜像", command=self.ctl.image_transpose)
        menu.add_command(label=" 缩放", command=self.ctl.Image_Scaling)
        return menu

    def menu_lqkpkgbv(self, parent):
        menu = Menu(parent, tearoff=False)
        menu.add_command(label="对比度", command=self.ctl.contrast)
        menu.add_command(label="饱和度", command=self.ctl.saturation)
        menu.add_command(label="曝光", command=self.ctl.exposure)
        menu.add_command(label="亮度", command=self.ctl.brightness)
        menu.add_command(label="光感", command=self.ctl.lightsense)
        menu.add_command(label="色调调节", command=self.ctl.hue)
        menu.add_command(label="色温调节", command=self.ctl.color_temperature)
        menu.add_command(label="HSL调节", command=self.ctl.adjust_hsl)
        menu.add_command(label="曲线调色", command=self.ctl.curve_toning)
        return menu

    def menu_tools(self, parent):
        menu = Menu(parent, tearoff=False)
        menu.add_command(label="平滑", command=self.ctl.smooth)
        menu.add_command(label="锐化", command=self.ctl.sharpen)
        menu.add_command(label="直方图均衡化", command=self.ctl.adjust_equalize)
        menu.add_command(label="文字", command=self.ctl.add_text)
        menu.add_command(label="水印", command=self.ctl.watermark)
        return menu

    def __event_bind(self):
        self.canvas.bind('<Enter>', self.ctl.hint)
        pass

    def configure_theme(self):
        style = Style()
        style.configure('TLabel', foreground='white', background='black')
        style.configure('TFrame', background='black')
        style.configure('TButton', foreground='white', background='black')
        style.configure('Vertical.TScrollbar', troughcolor='black', background='black', arrowcolor='white')
        style.configure('Horizontal.TScrollbar', troughcolor='black', background='black', arrowcolor='white')


if __name__ == "__main__":
    win = WinGUI()
    win.mainloop()


class CustomCanvas(tk.Canvas):
    def __init__(self, parent, width, height):
        super().__init__(parent, width=width, height=height, bg="white")
        self.pack()

        # 在画布中添加其他组件或布局
        # ...

    def add_frame_below(self, frame):
        # 获取画布的当前宽度和高度
        canvas_width = self.winfo_width()
        canvas_height = self.winfo_height()

        # 将 Frame 添加到画布的下方
        frame.place(x=0, y=canvas_height)

        # 更新画布的高度
        self.configure(height=canvas_height + frame.winfo_reqheight())